package cpa.prowon.gui.actions;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;

import cpa.prowon.Application;
import cpa.prowon.gui.MainWindow;
import cpa.prowon.image.ImageContainer;

/**
 * Import images which are selected in a file dialog prompter
 * @author Chris
 */
public class ImportSingleImagesAction extends ImportAction {

	/**
	 * Constructor
	 * @param MainWindow The window where the action is used
	 * @author Chris
	 */
	public ImportSingleImagesAction(MainWindow w) {
		super(w);
	}
	
	/**
	 * Return the title key
	 * @return The title key
	 * @author Chris
	 */
	protected String getTitleKey() {
		return "menu_item_file_importsingleimages";
	}

	/**
	 * Return the tooltip key
	 * @return The tootip key
	 * @author Chris
	 */
	protected String getTooltipKey() {
		return "menu_item_file_importsingleimages_tooltip";
	}

	/**
	 * Return the image key
	 * @return The image key
	 * @author Chris
	 */
	protected String getImageKey() {
		return "file:icons/import.gif";
	}

	/**
	 * Return the list of files (images) that shall be imported
	 * @return Collection The collection of images that will be imported
	 * @author Chris
	 */
	protected Collection getImagesToImport() {

		// Initialize the image list
		ArrayList imageList = new ArrayList ();		

		// Prompt for the directory
		FileDialog dialog = new FileDialog (window.getShell(), SWT.MULTI);
		dialog.setText(Application.getInstance().getResourceBundle().getString("msg_selectimagestoimport"));
		String[] filterNames = new String[1];
		String[] filterExtensions = new String[1];
		filterNames[0] = Application.getInstance().getResourceBundle().getString("filetype_jpeg");
		filterExtensions[0] = "*.jpg; *.jpeg";
		dialog.setFilterNames(filterNames);
		dialog.setFilterExtensions(filterExtensions);
		String firstFilePath = dialog.open();

		// Return if no directory was chosen
		if (firstFilePath == null) return imageList;

		// Get the file path
		File firstFile = new File(firstFilePath);
		String filePath = firstFile.getPath();
		int position = filePath.lastIndexOf("\\");
		filePath = filePath.substring(0,position+1);
		
		// Create the image list
		String[] result = dialog.getFileNames();
		for (int i=1; i <= result.length; i++) {
			ImageContainer ic = new ImageContainer(new File(filePath + result[i-1]));
			if (ic.isValid()) {
				imageList.add(ic);
			}
		}

		// Return the list of images
		return imageList;
		
	}

}
